1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2014 Devisualization (Richard Andrew Cattermole)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 module devisualization.util.opengl.camera;
25 import gl3n.linalg : mat3, vec3;
26 deprecated("de_util:opengl is going to die"):
27 
28 /**
29  * Provides a basic camera.
30  * 
31  * Based upon https://github.com/fogleman/pg/blob/master/pg/camera.py
32  */
33 struct Camera {
34     private {
35         vec3 position_;
36         float rx, ry;
37 
38         vec3 target_;
39     }
40 
41     this(vec3 position) {
42         position_ = position;
43     }
44 
45     this(vec3 position, vec3 target) {
46         position_ = position;
47         lookAt(target);
48     }
49 
50     @property {
51 
52         /**
53          * Configures the rotation to look at a point
54          * 
55          * Params:
56          *         t    =    The target to look at
57          */
58         void lookAt(vec3 t) {
59             import std.math : PI, atan2, asin;
60             target_ = t;
61 
62             //
63 
64             vec3 d = vec3(t.x - position_.x, t.y - position_.y, t.z - position_.z).normalized;
65             rx = 2 * PI - (atan2(d.x, d.z) + PI);
66             ry = asin(d.y);
67         }
68 
69         /**
70          * Configures the position of the matrix
71          * Also recalls lookAt given the previous target
72          * 
73          * Params:
74          *         position    =    The position to look from
75          */
76         void position(vec3 position) {
77             position_ = position;
78             lookAt(target_);
79         }
80 
81         vec3 position() {
82             return position_;
83         }
84 
85         vec3 lookAt() {
86             return target_;
87         }
88 
89         /**
90          * Gets the rotation matrix for current positon/rotation 
91          */
92         mat3 matrix(bool translate=true) {
93             import std.math : cos, sin;
94             mat3 matrix = mat3.identity;
95 
96 			if (translate)
97             	matrix = matrix.translate(-position_.x, -position.y, -position_.z);
98             matrix = matrix.rotate(ry, vec3(cos(rx), 0, sin(rx)));
99             matrix = matrix.rotate(-rx, vec3(0, 1, 0));
100 
101             return matrix;
102         }
103     }
104 }